package icu.lookyousmileface.concurrentutils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author starrysky
 * @title: CyclicBarrierUse
 * @projectName Juc_Pro
 * @description: 加法计数器
 * @date 2021/1/292:28 下午
 */
public class CyclicBarrierUse {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(6, () -> {
            System.out.println("召唤神龙！");
        });

        for (int i = 1; i <= 7; i++) {
            final int temp = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+":收集齐"+temp+"颗龙珠");
                try {
                    //此处的await是等待达到parties计数的时候，执行cyclicBarrier的线程，没有将不会执行
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
